package com.example.tellwin.question.presenter;

import com.example.tellwin.MainApplication;
import com.example.tellwin.api.TwjfApi;
import com.example.tellwin.base.RxPresenter;
import com.example.tellwin.base.SimpleCallback;
import com.example.tellwin.home.bean.ContentBean;
import com.example.tellwin.question.bean.UploadFileBean;
import com.example.tellwin.question.bean.VoiceBean;
import com.example.tellwin.question.contract.QuestionContract;
import com.example.tellwin.utils.HttpUtils;
import com.example.tellwin.view.LoadingDialog;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @author : LinZePeng
 * @date : 2020/12/4
 * @Describe ：
 **/
public class QuestionPresenter extends RxPresenter<QuestionContract.View> implements QuestionContract.Presenter<QuestionContract.View> {

    private TwjfApi mApi;

    private int photoPosition;
    private List<String> reqeuestPhotoPath;
    private List<String> resultPhotoUrl;
    private int voicePosition;
    private List<VoiceBean> voiceBeans;
    private List<VoiceBean> resultVoiceUrl;

    @Inject
    public QuestionPresenter(TwjfApi mApi) {
        this.mApi = mApi;
    }


    @Override
    public void getGradeList() {
        mApi.getGradeList(new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                if (contentBean != null && contentBean.getGradeList() != null) {
                    mView.gradeListResult(contentBean.getGradeList());
                }
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void getSubjectList() {
        mApi.getSubjectList(new SimpleCallback<ContentBean>(mView) {
            @Override
            public void onSuccess(ContentBean contentBean) {
                if (contentBean != null && contentBean.getSubjectList() != null) {
                    mView.subjectListResult(contentBean.getSubjectList());
                }
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void uploadPhoto(List<String> photoPaths) {
        reqeuestPhotoPath = photoPaths;
        if (reqeuestPhotoPath == null || reqeuestPhotoPath.size() <= photoPosition) {
            return;
        }
        resultPhotoUrl = new ArrayList<>();
        photoPosition = 0;
        uploadPhoto(photoPosition);
    }

    private void uploadPhoto(int position) {
        uploadFile(reqeuestPhotoPath.get(position), new SimpleCallback<String>(mView) {
            @Override
            public void onSuccess(String s) {
                resultPhotoUrl.add(s);
                photoPosition++;
                if (reqeuestPhotoPath.size() <= photoPosition) {//全部上传了
                    mView.uploadPhotoResult(resultPhotoUrl);
                    return;
                }
                uploadPhoto(photoPosition);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void uploadVoice(List<VoiceBean> voiceBeans) {
        this.voiceBeans = voiceBeans;
        if ( this.voiceBeans == null ||  this.voiceBeans.size() <= voicePosition) {
            return;
        }
        resultVoiceUrl = new ArrayList<>();
        voicePosition = 0;
        uploadVoice(voicePosition);
    }



    private void uploadVoice(int position){
        uploadFile(voiceBeans.get(position).getPath(), new SimpleCallback<String>(mView) {
            @Override
            public void onSuccess(String s) {
                VoiceBean voiceBean = new VoiceBean();
                voiceBean.setPath(s);
                voiceBean.setTime(voiceBeans.get(position).getTime());
                resultVoiceUrl.add(voiceBean);
                voicePosition++;
                if (voiceBeans.size() <= voicePosition) {//全部上传了
                    mView.uploadVocieResult(resultVoiceUrl);
                    return;
                }
                uploadVoice(voicePosition);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void uploadQuestion(List<String> photoUrls, List<VoiceBean> voiceUrls, String gradeId, String subjectId, String title, String content) {
        List<UploadFileBean>uploadFileBeans =new ArrayList<>();
        if(photoUrls!=null){
            for (int i = 0; i < photoUrls.size(); i++) {
                UploadFileBean uploadFileBean = new UploadFileBean("1" ,photoUrls.get(i));
                uploadFileBean.setFileSize("0");
                uploadFileBeans.add(uploadFileBean);
            }
        }
        if(voiceUrls!=null){
            for (int i = 0; i < voiceUrls.size(); i++) {
                UploadFileBean uploadFileBean = new UploadFileBean("2" ,voiceUrls.get(i).getPath());
                uploadFileBean.setFileSize(voiceUrls.get(i).getTime()+"");
                uploadFileBeans.add(uploadFileBean);
            }
        }
        Gson gson = new Gson();
        JsonArray jsonArray = gson.toJsonTree(uploadFileBeans, new TypeToken<List<UploadFileBean>>() {}.getType()).getAsJsonArray();
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("askTitle" , title);
        jsonObject.addProperty("describe" , content);
        jsonObject.addProperty("gradeId" , gradeId);
        jsonObject.addProperty("subjectId" , subjectId);
        jsonObject.add("orderFile" , jsonArray);

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());

        mApi.uploadQuestion(requestBody , new SimpleCallback<Object>(mView) {
            @Override
            public void onSuccess(Object o) {
                mView.uploadQuestionSuccess();
            }

            @Override
            public void onFail(String msg) {

            }
        });

    }

    private void uploadFile(String path, SimpleCallback<String> stringSimpleCallback) {
        File file = new File(path);
        String name = file.getName();
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", name, RequestBody.create(MediaType.parse("*/*"), file)) // 第一个参数传到服务器的字段名，第二个你自己的文件名，第三个MediaType.parse("*/*")和我们之前说的那个type其实是一样的
                .build();
        mApi.uploadFile(requestBody, stringSimpleCallback);
    }
}
